/*-------------------------------------------------------------------------
 *
 * barrier.h
 *	  用于同步协作进程的障碍。
 *
 * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
 * Portions Copyright (c) 1994, Regents of the University of California
 *
 * src/include/storage/barrier.h
 *
 *-------------------------------------------------------------------------
 */
#ifndef BARRIER_H
#define BARRIER_H

/*
 * 对于之前被称为“barrier.h”的头文件，请包含
 * “port/atomics.h”，该文件处理原子操作、编译器屏障和内存
 * 屏障。
 */

#include "storage/condition_variable.h"
#include "storage/spin.h"

typedef struct Barrier
{
	slock_t		mutex;
	int			phase;			/* 阶段计数器 */
	int			participants;	/* 附加参与者的数量 */
	int			arrived;		/* 已经到达的参与者数量 */
	int			elected;		/* 选举出的最高阶段 */
	bool		static_party;	/* 仅用于断言 */
	ConditionVariable condition_variable;
} Barrier;

extern void BarrierInit(Barrier *barrier, int num_workers);
extern bool BarrierArriveAndWait(Barrier *barrier, uint32 wait_event_info);
extern bool BarrierArriveAndDetach(Barrier *barrier);
extern bool BarrierArriveAndDetachExceptLast(Barrier *barrier);
extern int	BarrierAttach(Barrier *barrier);
extern bool BarrierDetach(Barrier *barrier);
extern int	BarrierPhase(Barrier *barrier);
extern int	BarrierParticipants(Barrier *barrier);

#endif							/* BARRIER_H */
